﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Business.Compta;
using DAO.Compta;
using Manageo;

namespace Compta
{
    /// <summary>
    /// Interaction logic for ComptaConsommationsUI.xaml
    /// </summary>
    public partial class ComptaConsommationsUI : UserControl
    {
        private ObservableRangeCollection<ComptaConsommation> allConsos = new ObservableRangeCollection<ComptaConsommation>();

        public ObservableRangeCollection<ComptaConsommation> AllConsos
        {
            get
            {
                if (allConsos == null)
                {
                    this.allConsos = new ObservableRangeCollection<ComptaConsommation>(ComptaConsommationsDAO.getAllConsommations());
                }
                return allConsos;
            }
        }

        public ComptaConsommationsUI()
        {
            this.allConsos = new ObservableRangeCollection<ComptaConsommation>(ComptaConsommationsDAO.getAllConsommations());
            InitializeComponent();
        }


        private void view_ValidateRow(object sender, DevExpress.Wpf.Grid.GridRowValidationEventArgs e)
        {
            ComptaConsommation conso = (ComptaConsommation)e.Row;
            if (conso != null)
            {
                e.IsValid = (conso.Prix > 0 || conso.Volume > 0) && conso.Date != null;
                if (e.IsValid && conso != null)
                {
                    if (conso.Id == -1)
                    {
                        try
                        {
                            conso.Id = ComptaConsommationsDAO.InsertConsommation(conso);
                            conso.commit();
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(Window.GetWindow(this), "error while modifying or insert consommation ! (id = " + conso.Id + ")\n" + ex, "Error !!!", MessageBoxButton.OK, MessageBoxImage.Error);
                            allConsos.Remove(conso);
                        }
                    }
                    else
                    {
                        try
                        {
                            ComptaConsommationsDAO.ModifyConsommation(conso);
                            conso.commit();
                        }
                        catch (Exception ex)
                        {
                            conso.reject();
                            MessageBox.Show("error while modifying or insert compta line ! (id = " + conso.Id + ")\n" + ex, "Error !!!", MessageBoxButton.OK, MessageBoxImage.Error);
                        }
                    }
                }
                else
                {
                    e.ErrorContent = "Date and Amount must not be null.\n\n";
                }
            }
        }

        private void keyUp(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Delete)
            {
                DevExpress.Wpf.Grid.TableView view = (DevExpress.Wpf.Grid.TableView)sender;
                if (view.ActiveEditor == null || !view.ActiveEditor.IsEditorActive)
                {

                }
            }
        }

        private void gridControl1_CustomUnboundColumnData(object sender, DevExpress.Wpf.Grid.GridColumnDataEventArgs e)
        {
            if (e.IsGetData)
            {
                double volume = (double)e.GetListSourceFieldValue("Volume");
                double prix = (double)e.GetListSourceFieldValue("Prix");
                e.Value = volume * prix;
            }
        }
        
    }
}
